package main

import (
	"bufio"
	"controller-client/client"
	"controller-client/config"
	"fmt"
	"log"
	"os"
	"os/signal"
	"strings"
	"syscall"
)

func main() {
	tcpClient := client.Start(config.AppConf.ServerConfig.Addr)
	go tcpClient.DoReceive()
	//写一个冲控制台读入string程序
	scanner := bufio.NewScanner(os.Stdin)
	fmt.Println("选择操作:")
	fmt.Println("login [username] [password]")
	fmt.Println("register [username] [nickname] [password]")
	fmt.Println("send [username] [content]")
	for scanner.Scan() {
		command := scanner.Text()
		parts := strings.Split(command, " ")
		if len(parts) > 0 {
			switch parts[0] {
			case "login":
				tcpClient.DoLogin(parts[1], parts[2])
			case "register":
				tcpClient.Register(parts[1], parts[2], parts[3])
			case "send":
				tcpClient.PrivateChat(parts[1], parts[2])
			}
		}
	}
	//这边主线程一直等待
	quite := make(chan os.Signal)
	signal.Notify(quite, syscall.SIGINT, syscall.SIGKILL, syscall.SIGQUIT)
	//阻塞在这边等待系统信号关闭
	<-quite
	err := tcpClient.Close()
	if err != nil {
		log.Printf("close tcp client: %v\n", err)
	}
	log.Printf("shut down tcp client\n")
}
